/*
 * HttpRestServer.h
 *
 *  Created on: 2022-05-08
 *      Author: chuanjiang.zh
 */

#ifndef HTTPRESTSERVER_H_
#define HTTPRESTSERVER_H_

#include "BasicType.h"
#include "HttpServer.h"

#include "HttpFactory.h"
#include "json/json.h"
#include "JsonUtil.h"
#include "RestHttpHandler.h"
#include "DbJsonConnection.h"

class HttpRestServer : public RestHttpHandler
{
public:
    HttpRestServer();
    virtual ~HttpRestServer();

    virtual bool start(int port);

    virtual void stop();

    virtual bool isStarted();


    virtual HttpServerPtr getServer();


public:

public:
    static bool bodyAsJson(HttpRequest& request, Json::Value& json);
    static bool parseJson(const char* str, size_t len, Json::Value& json);

    static int setResult(const Json::Value& json, HttpResult& result);
    static int setResult(HttpResult& result, const Json::Value& json);
    static int setResult(HttpResult& result, const db::JsonList& jsonList);
    static int setResult(HttpResult& result, int offset, int limit, int total, const db::JsonList& jsonList);

    static int setResultData(HttpResult& result, const Json::Value& json);

    int setResult(HttpResult& result, bool got);

    template < class T>
    static int setResultObject(HttpResult& result, const T& t)
    {
        Json::Value value;
        Json::toJson(t, value);
        return setResultData(result, value);
    }

    int query(db::DbJsonConnection& conn, const std::string& sql, HttpResult& result);
    int querySome(db::DbJsonConnection& conn, const std::string& sql, HttpResult& result);
    int execute(db::DbJsonConnection& conn, const std::string& sql, HttpResult& result);

protected:
    virtual void setupUri();



protected:


protected:
    HttpServerPtr   m_server;

};

#endif /* HTTPRESTSERVER_H_ */
